package com.ghy.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.ghy.dto.NoticeInsertDTO;
import com.ghy.dto.NoticePageDTO;
import com.ghy.dto.NoticeUpdateDTO;
import com.ghy.entity.Notice;

import com.ghy.vo.NoticeSimpleListVO;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.relation.RelationManager;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import com.ghy.mapper.NoticeMapper;
import com.ghy.service.NoticeService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import java.util.List;

import static com.ghy.entity.table.NoticeTableDef.NOTICE;


/**
 * 通知表 服务层实现。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Service
@CacheConfig(cacheNames = "notice")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>  implements NoticeService{


    @CacheEvict(allEntries = true)
    @Override
    public boolean insert(NoticeInsertDTO dto) {
        //组装entity 实体类
        Notice notice = BeanUtil.copyProperties(dto, Notice.class);
       
        notice.setCreated(LocalDateTime.now());
        notice.setUpdated(LocalDateTime.now());

        return mapper.insert(notice) > 0;
    }

    @Cacheable(key = "#p0",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public Notice select(Long id) {
        RelationManager.addQueryRelations("episode");
        return mapper.selectOneWithRelationsById(id);
    }

    @Cacheable(key = "#root.methodName",
            unless = "#result == null")
    @Override
    public List<NoticeSimpleListVO> simpleList() {
        return QueryChain.of(mapper)
                .withRelations ()
                .listAs(NoticeSimpleListVO.class);
    }

    @Cacheable(key ="#root.methodName + ':' + #p0.toString()",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public Page<Notice> page(NoticePageDTO dto) {
        QueryChain<Notice> queryChain = QueryChain.of(mapper);

        return queryChain.withRelations()
                .page(new Page<>(dto.getPageNum(),dto.getPageSize()));
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(NoticeUpdateDTO dto) {
        //组装entity实体类
        Notice notice = BeanUtil.copyProperties(dto, Notice.class);

        notice.setUpdated(LocalDateTime.now());

        return UpdateChain.of(notice)
                .where(NOTICE.ID.eq(notice.getId()))
                .update();
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public boolean delete(Long id) {

        //删除父评论
        return mapper.deleteById(id) > 0;

    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public boolean deleteBatch(List<Long> ids) {

        //删除父评论
        return mapper.deleteBatchByIds(ids) > 0;
    }

    @CacheEvict(allEntries = true)
    @Cacheable(key = "#root.methodName", unless = "#result == null")
    @Override
    public List<Notice> top(Long n) {
        return QueryChain.of(mapper)
                .orderBy(NOTICE.IDX.asc(), NOTICE.ID.desc())
                .limit(n)
                .withRelations()
                .list();
    }
}
